package edu.zust.zw.shop.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class CrossHandlerInterceptor
		implements HandlerInterceptor /* extends HandlerInterceptorAdapter */ {
	private Logger logger = Logger.getLogger(CrossHandlerInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String origin = request.getHeader("Origin");

		response.setHeader("Access-Control-Allow-Origin", origin); // 第二个参数填写允许跨域的域名称，不建议直接写
																	// "*"
		response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
		response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");

		// 接收跨域的cookie
		response.setHeader("Access-Control-Allow-Credentials", "true");
		logger.info("进入跨域拦截器");

		return true;

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
